/**
 * 
 */
package be.redo.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import be.redo.model.entities.Continent;
import be.redo.model.entities.Region;

/**
 * @author Frank
 * 
 */
@Repository
public class RegionDAOImpl implements RegionDAO {
	final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private EntityManager entityManager;

	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	/**
	 * @see be.redo.dao.RegionDAO#create(be.redo.model.entities.Region)
	 */
	@Override
	public void create(Region instance) {
		logger.debug(" create("+instance+")");
		try {
			entityManager.persist(instance);
		} catch (RuntimeException ex) {
			entityManager.clear();
			throw ex;
		}
	}

	/**
	 * @see be.redo.dao.RegionDAO#read(long)
	 */
	@Override
	public Region read(long id) {
		logger.debug(" read("+id+")");
		return entityManager.find(Region.class, id);
	}

	/**
	 * @see be.redo.dao.RegionDAO#update(be.redo.model.entities.Region)
	 */
	@Override
	public void update(Region instance) {
		logger.debug(" update("+instance+")");
		try {
			entityManager.merge(instance);
			entityManager.flush();
		} catch (RuntimeException ex) {
			entityManager.clear();
			throw ex;
		}
	}

	/**
	 * @see be.redo.dao.RegionDAO#delete(long)
	 */
	@Override
	public void delete(long id) {
		logger.debug(" delete("+id+")");
		Region instance = entityManager.find(Region.class, id);
		if (instance != null) {
			entityManager.remove(instance);
			entityManager.flush();
		}
	}

	/**
	 * @see be.redo.dao.RegionDAO#findAll()
	 */
	@Override
	public List<Region> findAll() {
		logger.debug(" findAll");
		TypedQuery<Region> query = entityManager.createNamedQuery(
				"findAllRegions", Region.class);
		return query.getResultList();
	}

	/**
	 * @see be.redo.dao.RegionDAO#count()
	 */
	@Override
	public int count() {
		logger.debug(" count");
		TypedQuery<Number> query = entityManager.createNamedQuery(
				"findAantalRegions", Number.class);
		return (query.getSingleResult()).intValue();
	}

	/**
	 * @see be.redo.finders.RegionFinders#findRegionsByNameLike(java.lang.String)
	 */
	@Override
	public List<Region> findRegionsByNameLike(String name) {
		logger.debug(" findRegionsByNameLike("+name+")");
		TypedQuery<Region> query = entityManager.createNamedQuery(
				"findRegionsByNameLike", Region.class);
		query.setParameter("name", name);
		return query.getResultList();
	}

	/**
	 * @see be.redo.finders.RegionFinders#findRegionsByContinent(be.redo.model.entities.Continent)
	 */
	@Override
	public List<Region> findRegionsByContinent(Continent continent) {
		logger.debug(" findRegionsByContinent("+continent+")");
		TypedQuery<Region> query = entityManager.createNamedQuery(
				"findRegionsByContinent", Region.class);
		query.setParameter("continent", continent);
		return query.getResultList();
	}

	/**
	 * @see be.redo.finders.RegionFinders#findRegionsByFirstCharactersOfName(java.lang.String)
	 */
	@Override
	public List<Region> findRegionsByFirstCharactersOfName(String name) {
		logger.debug(" findRegionsByFirstCharactersOfName("+name+")");
		TypedQuery<Region> query = entityManager.createNamedQuery(
				"findRegionsByFirstCharactersOfName", Region.class);
		query.setParameter("name", name);
		return query.getResultList();
	}
}
